<?php
namespace Home\Controller;
use Think\Controller;
//use Think\PHPExcel;

class IndexController extends Controller {
    //ThinkPHP的构造函数
    public function _initialize() {
        //自动加载的类
        new \Org\Admin_autoload();
    }
    //后台首页
    public function index(){
        $data['title']='系统概览';
        $data['active']='index_index';
        $this->assign("data", $data);

        $Model=D("Article");
        $article_list = $Model->get_allvote_list(1);//节目排名
        $article_vote_list = $Model->get_allvote_list(2);//演员排名

        $this->assign("article_list", $article_list);
        $this->assign("article_vote_list", $article_vote_list);

        $this->display();
    }

    //关于
    public function about(){
        $data['title']='关于我们通用后台';
        $this->assign("data", $data);
        $this->display();
    }

    //系统设置
    public function setup(){
        $setup=M('Setup')->field('value')->select();
        $this->assign("setup", $setup);
        $data['title']='系统设置';
        $data['active']='index_setup';
        $this->assign("data", $data);
        $this->display();
    }

    //系统设置提交
    public function setup_post(){
        //提交检测
        if($_POST['start_time']==''||$_POST['end_time']==''){
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
        $arr=I('post.');
        //一些数据检测
        //session('admin_category_id',$arr['category_id']);
        //session('admin_article_sex',$arr['article_sex']);
        //更新数据库
        //dump($arr);exit;
        $result1=M('Setup')->where('id=1')->save(array('value'=>$arr['meta']));
        $result2=M('Setup')->where('id=2')->save(array('value'=>$arr['start_time']));
        $result3=M('Setup')->where('id=3')->save(array('value'=>$arr['end_time']));
        if($result1===false||$result2===false||$result3===false){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }else{
            //echo "<script>alert('提交成功');window.location.href='/article/index/article_add';</script>";
            echo "<script>window.location.href='/home/index/setup';</script>";
        }
    }

    //导入会员
    public function import(){
        $data['title']='导入会员';
        $data['active']='index_import';
        $this->assign("data", $data);
        $this->display();
    }

    //导入会员
    public function import_post(){
        if(empty($_FILES['excel_file']['tmp_name'])){
            echo "<script>alert('您没有选择文件');history.back(-1);</script>";exit;
        }
        $root_path = ROOT_PATH . 'Data/upload/excel';
        if (!is_dir($root_path)) {
            mkdir($root_path);
            chmod($root_path, 0777);
        }
        //上传
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 10048000;// 设置附件上传大小 10M
        $upload->exts = array('xls', 'xlsx');// 设置附件上传类型
        $upload->rootPath = $root_path . '/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->subName = '';
        //$upload->saveName='purchase_'.md5(time()).'_'.rand(001,999);
        $upload->saveName = 'uniqid';
        $info = $upload->upload();
        if (!$info) {// 上传失败
            echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";
            exit;
        }
        $excel_file=$root_path.'/'.$info['excel_file']['savename'];
        //dump($excel_file);exit;
        //读取excel 原始数据
        vendor('Excelreader.Reader');
        vendor('Excelreader.Oleread');
        $Reader=new \Spreadsheet_Excel_Reader(); //实例化
        $Reader->setOutputEncoding('utf-8');      //编码
        $Reader->read( $excel_file);            //读取的文件
        $excel_data_list=$Reader->sheets[0]['cells'];
        //dump($excel_data_list);exit;
        //写入数据库
        $data=array();
        $i=0;
        foreach($excel_data_list as $k=>$v){
            if($k>1){
                //echo '姓名:'.$v[1].'  身份证号:'.$v[2].'<br />';
                $data['name']=$v[2];
                $data['department1']=$v[4];
                $data['department2']=$v[3];
                $data['identity']=$v[5];
                $data['mobile']=$v[6];
                $data['type']=$v[7];
                $result=M('user_book')->add($data);
                if($result===false){
                    echo 'error';exit;
                }
                $i++;
            }
        }
        echo "<script>alert('提交成功');window.location.href='/home/index/import';</script>";exit;
    }

}